<template>
  <div class="innerContainer">
    <common-title text="消息配置">
      <template #expand>
        <el-button type="primary" @click="add">新增</el-button>
      </template>
    </common-title>

    <div class="innerPane">
      <div class="filterArea">
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="消息主题">
            <el-input @keyup.enter="search" v-model="searchForm.mqttTopic" placeholder="请搜索消息主题"></el-input>
          </el-form-item>
          <el-form-item label="IP:PORT">
            <el-input @keyup.enter="search" v-model="searchForm.mqttUri" placeholder="请搜索IP:PORT"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search">查询</el-button>
            <el-button @click="onClear">重置</el-button>
          </el-form-item>
        </el-form>
      </div>


      <el-table :data="tableData" border height="100%" :cell-style="{ textAlign: 'center' }" row-class-name="fixed-row">
        <el-table-column header-align="center" prop="mqttUri" label="IP:PORT" width="220px" />
        <el-table-column header-align="center" prop="mqttUser" label="用户名" width="120px" />
        <el-table-column header-align="center" prop="mqttQos" label="服务质量" width="120">
          <template #default="scope">
            {{ getText(scope.row) }}
          </template>
        </el-table-column>


        <el-table-column header-align="center" prop="mqttTopic" label="消息主题" />
        <el-table-column header-align="center" prop="rootDir" label="文件存储目录" />
        <!-- <el-table-column
            header-align="center"
            prop="shardSize"
            label="	分片大小"
            width="80"
          >
            <template #default="scope"> {{ scope.row.shardSize }}kb </template>
          </el-table-column> -->
        <el-table-column header-align="center" prop="remark" label="备注" show-overflow-tooltip />

        <el-table-column label="操作" header-align="center" width="200" prop="col9">
          <template #default="scope">
            <el-link type="primary" size="small" v-if="scope.row.useStatus === 0" @click="enableInfo(scope.row)">
              启用
            </el-link>
            <el-link type="primary" size="small" v-if="scope.row.useStatus === 1" @click="disableInfo(scope.row)">
              禁用
            </el-link>
            <el-link type="primary" size="small" @click="edit(scope.$index, scope.row)">
              编辑
            </el-link>
            <el-link type="primary" size="small" @click="del(scope.row)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination style="margin-top: 20px" :current-page="searchForm.current" :page-size="searchForm.size"
        :page-sizes="[10, 20, 30, 40]" layout="->,total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>

    <mqtt-view ref="mqttView" @confirm="search"></mqtt-view>
  </div>
</template>

<script setup>
import CommonTitle from "@/components/CommonTitle.vue";
import {
  getMqTable,
  delMqTable,
  enableMq,
  disableMq,
  getConfigParams,
} from "../../utils/requests";
import { onMounted, reactive, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";

import MqttView from "./view.vue";

const router = useRouter();

let messageLevelList = ref();

let shardList = ref();

// Dom 挂载之后
onMounted(async () => {
  messageLevelList.value = await getParams("mqtt.message.qos");
  shardList.value = await getParams("mqtt.buffer.shard");
  search();
});

const getParams = async (params) => {
  let res = await getConfigParams({
    cats: params,
  });
  return res;
};

// 用户数据
let tableData = ref([]);

let total = ref(0);
// 搜索条件
const searchForm = ref({
  mqttTopic: "",
  mqttUri: "",
  current: 1,
  size: 10,
});

const onClear = () => {
  searchForm.value.mqttTopic = "";
  searchForm.value.mqttUri = "";
  search();
};

const handleSizeChange = (size) => {
  searchForm.value.size = size;
  search();
};
const handleCurrentChange = (current) => {
  searchForm.value.current = current;
  search();
};
const getText = (rowData) => {
  if (!rowData || !messageLevelList.value || !messageLevelList.value.length)
    return "";
  return messageLevelList.value.filter(
    (item) => parseInt(item.value) === rowData.mqttQos
  )[0].text;
};

const tableRowClassName = (scope) => {
  if (scope.row.useStatus === 0) {
    return "font-gray";
  }
  return "";
};

const search = () => {
  getMqTable(searchForm).then((res) => {
    tableData.value = res.records;

    total.value = res.total;
  });
};

// 详情查看弹窗
const mqttView = ref();

//添加
const add = () => {
  mqttView.value.openDialog({
    rowData: null,
    messageLevelList: messageLevelList.value,
    shardList: shardList.value,
  });
};

//编辑
const edit = (index, row) => {
  let rowData = Object.assign({ _o: "w" }, row);
  mqttView.value.openDialog({
    rowData: rowData,
    messageLevelList: messageLevelList.value,
    shardList: shardList.value,
  });
};

// 删除用户
const del = (rowData) => {
  ElMessageBox.confirm("确定要删除该条信息吗?", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      const res = await delMqTable({ ids: rowData.id });

      ElMessage.success("删除成功");
      setTimeout(() => {
        search();
      }, 0);
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};

const enableInfo = (rowData) => {
  ElMessageBox.confirm("确定要启用该条信息吗?", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      const res = await enableMq({ id: rowData.id });

      ElMessage.success("启用成功");
      setTimeout(() => {
        search();
      }, 0);
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消启用",
      });
    });
};

const disableInfo = (rowData) => {
  ElMessageBox.confirm("确定要禁用该条信息吗?", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      const res = await disableMq({ id: rowData.id });

      ElMessage.success("禁用成功");
      setTimeout(() => {
        search();
      }, 0);
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消禁用",
      });
    });
};
</script>

<style scoped></style>
